﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using WebEntities;
using WebEntities.Models.WebInfo;
using com.subb.WebInformation;

namespace WebsiteAdmin.Controllers
{
    [Authorize]
    public class LeaveMsgController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetAllLeaveMsgByPage(int typeId, StoreRequestParameters parms)
        {
            WI_LeaveMsgDAL lmdal = new WI_LeaveMsgDAL();
            WI_LeaveMsgTypeDAL mtdal = new WI_LeaveMsgTypeDAL();
            int ttlCount;
            List<WI_LeaveMsg> wilmsgList;
            if (typeId == 0)
            {
                //获取全部
                wilmsgList = lmdal.GetAllLeaveMsg(parms.Start, parms.Limit, out ttlCount);
            }
            else
            {
                //按留言类别获取
                wilmsgList = lmdal.GetAllLeaveMsg(typeId, parms.Start, parms.Limit, out ttlCount);
            }
            List<LeaveMsg> lmsgList = new List<LeaveMsg>();
            foreach (var wilmsg in wilmsgList)
            {
                string mtypeName = string.Empty;
                if (wilmsg.TypeID.HasValue)
                {
                    WI_LeaveMsgType mtype = mtdal.GetMsgType(wilmsg.TypeID.Value);
                    if (mtype != null)
                    {
                        mtypeName = mtype.MsgType;
                    }
                }
                LeaveMsg lmsg = LeaveMsg.AsLeaveMsg(wilmsg, mtypeName);
                lmsgList.Add(lmsg);
            }
            return this.Store(lmsgList, ttlCount);
        }

        [HttpPost]
        public ActionResult DeleteLeaveMsg(List<LeaveMsg> delMsgs)
        {
            try
            {
                WI_LeaveMsgDAL lmdal = new WI_LeaveMsgDAL();
                foreach (var msg in delMsgs)
                {
                    lmdal.RemoveLeaveMsg(msg.MsgID);
                }
                return this.Direct(new { Success = true, Msg = "删除成功！" });
            }
            catch (Exception ex)
            {
                return this.Direct(new { Success = false, Msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message });
            }
        }

        public ActionResult EditMsgWindow(int msgId)
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "查看用户留言",
                Width = 700,
                Height = 500,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.Script,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("EditLeaveMsg"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Loader.Params.Add(new Parameter("msgId", msgId.ToString()));
            wnd.Render();
            return this.Direct();
        }

        public ActionResult EditLeaveMsg(int msgId)
        {
            WI_LeaveMsgDAL lmdal = new WI_LeaveMsgDAL();
            WI_LeaveMsg wilmsg = lmdal.GetLeaveMsg(msgId);
            string mtypeName = string.Empty;
            if (wilmsg.TypeID.HasValue)
            {
                WI_LeaveMsgTypeDAL mtdal = new WI_LeaveMsgTypeDAL();
                WI_LeaveMsgType mtype = mtdal.GetMsgType(wilmsg.TypeID.Value);
                if (mtype != null)
                {
                    mtypeName = mtype.MsgType;
                }
            }
            LeaveMsg lmsg = LeaveMsg.AsLeaveMsg(wilmsg, mtypeName);
            lmsg.MsgBody = Server.HtmlDecode(lmsg.MsgBody);
            ViewBag.IsPublicTxt = (lmsg.IsPublic ?? false) ? "已发布" : "审核未通过";
            ViewBag.IsReplyTxt = (lmsg.IsReply ?? false) ? "已回复" : "暂未回复";

            //获取回复内容
            if (wilmsg.IsReply ?? false)
            {
                WI_LeaveMsgReplyDAL mrdal = new WI_LeaveMsgReplyDAL();
                WI_LeaveMsgReply wireply = mrdal.GetReplyMsgByMsgID(wilmsg.MsgID);
                if (wireply != null)
                {
                    ViewBag.ReplyContent = Server.HtmlDecode(wireply.ReplyContent);
                    ViewBag.ReplyMan = Server.HtmlDecode(wireply.ReplyMan);
                }
            }
            return View(lmsg);
        }

        [HttpPost]
        public ActionResult AuditLeaveMsg(int msgId, bool isPub)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                string auditor = Request.IsAuthenticated ? HttpContext.User.Identity.Name : "Anonymous";
                WI_LeaveMsgDAL lmdal = new WI_LeaveMsgDAL();
                lmdal.AuditLeaveMsg(msgId, isPub, auditor);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "审核完成！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }

        [HttpPost]
        public ActionResult BatchAuditLeaveMsg([ModelBinder(typeof(JsonModelBinder))]Dictionary<string, string>[] leaveMsgs, bool isPub)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                string auditor = Request.IsAuthenticated ? HttpContext.User.Identity.Name : "Anonymous";
                WI_LeaveMsgDAL lmdal = new WI_LeaveMsgDAL();
                foreach (var lmsg in leaveMsgs)
                {
                    int msgId;
                    if (int.TryParse(lmsg["MsgID"],out msgId))
                    {
                        lmdal.AuditLeaveMsg(msgId, isPub, auditor);
                    }
                }
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "审核完成！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }
    }
}
